**# DOCUMENTATION
/*
 Part 1: Use Zephyr dataset as input and generate 2 outputs: (a) a clean list of deals (b) an aggregated measures of M&A activity.
 Part 2: Use the output of Part 1 and MERGE it with the Regulatory Scope for aggregated analyses.					 
 Part 3: Use the output of Part 1 and MERGE it with the Regulatory Scope for deal level analyses.				 
 Part 4: Use the output of Part 1 and MERGE it with the Regulatory Scope target analises.

*/

**# Setting and Configurations
cls 
clear
set more off
global path_project "/Mandatory disclosure and M&A/"
global path_RR "/Mandatory disclosure and M&A/Marcelo_Jupyter/"
global path_orbis "/Orbis Data/"
global start_year = 2000
 
**# PART 1

cd "${path_RR}"
use "${path_project}/Data/1_zephyr.dta"   //the same deals than in Borja, Tapia and Urzua (2017)
keep if t_country=="AT"|t_country=="BE"|t_country=="DE"|t_country=="DK"|t_country=="ES"|t_country=="FI"|t_country=="FR"|t_country=="GB"|t_country=="IE"|t_country=="IT"|t_country=="NL"|t_country=="SE"
gen country=""
replace country="Austria"  if t_country=="AT"
replace country="Belgium"  if t_country=="BE"
replace country="Germany"  if t_country=="DE"
replace country="Denmark"  if t_country=="DK"
replace country="Spain"  if t_country=="ES"
replace country="Finland"  if t_country=="FI"
replace country="France"  if t_country=="FR"
replace country="United Kingdom"  if t_country=="GB"
replace country="Ireland"  if t_country=="IE"
replace country="Italy"  if t_country=="IT"
replace country="Netherlands"  if t_country=="NL"
replace country="Sweden"  if t_country=="SE"
drop if deal_year < ${start_year}
drop if missing(country)
rename deal_year year
joinby deal  using "${path_project}/Data/Updates from Zephyr/deals_data.dta",    update unm(master)			
drop _merge 
//  Drop if we cannot measure the control transference 
drop if missing(initial) & missing(final) & missing(acquired) 
drop if missing(initial) & missing(acquired)
drop if missing(final) & missing(acquired)
duplicates drop deal year, force 
// Merging to obtain targets' info from  historical Orbis 
joinby deal  using "${path_project}/Data/Zephyr oldOrbis/Zephyr_old_orbis.dta", update unm(master)
drop _merge 
tab  t_slegalf t_slegalf_oldorbis  //to compare how many "legal status=Unknown". 
replace t_slegalf= "Private limited companies" if  t_slegalf=="Unknown" & t_slegalf_oldorbis=="Private limited companies" 
replace t_slegalf= "Public limited companies" if  t_slegalf=="Unknown" & t_slegalf_oldorbis=="Public limited companies" 
replace t_slegalf= "Other" if t_slegalf=="Unknown" & t_slegalf_oldorbis=="Other" 
tab  t_slegalf   // 
rename targetlegalform_oldorbis t_legalform_oldorbis
rename t_bvid bvid
rename year closdate_year
replace closdate_year=closdate_year-1                            // for finding legal data one year before the merge (some IDs are missing in the deal year)

cd "${path_orbis}"  
local country AT BE DE DK ES FI FR GB IE IT NL SE 
foreach c of local country {	
		display "Searching Orbis' data for country `c'"
		joinby bvid closdate_year using 1_Code_sample_extended_`c'.dta, update unm(master)
		drop   bvid2  siclength  alwaysalive  new dead   turn       toas_priv toas_privfull toas_publ sales_priv sales_privfull sales_publ conscode private public other
		drop _merge
	}
// rename the recently added variables from Orbis, and use them to update t_slegalf
rename slegalf t_slegalf_orbis
rename toas t_toas_orbis
rename empl t_empl_orbis
rename opre t_opre_orbis
rename fulldisclosure t_fulldisclosure
replace t_slegalf= "Private limited companies" if  t_slegalf=="Unknown" & t_slegalf_orbis=="Private limited companies" 
replace t_slegalf= "Public limited companies" if  t_slegalf=="Unknown" & t_slegalf_orbis=="Public limited companies" 
replace t_slegalf= "Other" if t_slegalf=="Unknown" & !missing(t_slegalf_orbis) & t_slegalf_orbis!="Private limited companies" & t_slegalf_orbis!="Public limited companies"
tab  t_slegalf       
replace t_sic=sic if missing(t_sic)    
drop  nacepcod2 listed listedd toas_listed     dateinc_year sales sic*

// Merging with Updated Orbis (BIG QUERY) to get firm-size classification variables and also legal status variables for ACQUIRERS
rename  bvid t_bvid
rename a_bvid bvid
local country AT BE DE DK ES FI FR GB IE IT NL SE 
foreach c of local country {	
		display "Searching Orbis' data for country `c'"
		joinby bvid closdate_year using 1_Code_sample_extended_`c'.dta, update unm(master)
		drop   bvid2  siclength  alwaysalive  new dead   turn       toas_priv toas_privfull toas_publ sales_priv sales_privfull sales_publ conscode private public other
		drop _merge
	}
	
cd "${path_RR}/Data/Zephyr_extended"
// rename the recently added variables from Orbis, and use them to update a_slegalf
rename slegalf a_slegalf_orbis
rename toas a_toas_orbis
rename empl a_empl_orbis
rename opre a_opre_orbis
rename fulldisclosure a_fulldisclosure
rename  bvid a_bvid
replace closdate_year=closdate_year+1
replace a_slegalf= "Private limited companies" if  a_slegalf=="Unknown" & a_slegalf_orbis=="Private limited companies" 
replace a_slegalf= "Public limited companies" if  a_slegalf=="Unknown" & a_slegalf_orbis=="Public limited companies" 
replace a_slegalf= "Other" if a_slegalf=="Unknown" & !missing(a_slegalf_orbis) & a_slegalf_orbis!="Private limited companies" & a_slegalf_orbis!="Public limited companies"
replace a_sic=sic if missing(a_sic)  
drop  nacepcod2 listed listedd toas_listed     dateinc_year sales sic*

// Correcting indutries code format [
gen siclength=length(t_sic)
replace t_sic = t_sic +"0" if siclength==3   //in this file, the codes are aggregated at the "primary code", so the 4digits code 1234 may be aggregated at the first 2digits (e.g, "12")
replace t_sic =  t_sic + "00" if siclength==2
gen sic_3d=substr(t_sic,1,3)
gen sic_2d=substr(t_sic,1,2)
destring sic_3d, gen(sic_3d_2)
destring sic_2d, gen(sic_2d_2)
drop siclength
gen siclength=length(a_sic)
replace a_sic = a_sic +"0" if siclength==3   //in this file, the codes are aggregated at the "primary code", so the 4digits code 1234 may be aggregated at the first 2digits (e.g, "12")
replace a_sic =  a_sic + "00" if siclength==2
gen a_sic_3d=substr(a_sic,1,3)
gen a_sic_2d=substr(a_sic,1,2)
destring a_sic_3d, gen(a_sic_3d_2)
destring a_sic_2d, gen(a_sic_2d_2)
drop siclength
drop if missing(t_sic)
drop if missing(a_sic) // nuevo
*save "1_zephyr_clean_temp.dta", replace		
//  Control Transference Restriction
gen change_control= (initial<=50 & final >50)  			// changes in control (Rossi & Volpin JFE 2004, Duchin & Smith JFE 2013)
replace change_control=1 if  !missing(acquired) & acquired>50 //for the few cases with "acquired" but some missing in "initial" or "final" 
replace change_control=1  if t_slegalf== "Public limited companies"  & acquired>=10 & final >=20  
drop if change_control!=1
// Classification by disclosure requirements : pre_asset<threshold
gen t_priv_fulldisc= (t_slegalf=="Private limited companies" & t_fulldisclosure==1)  // "fulldisclosure" uses orbis historical data to classify firms, with toas, sales and empl.
replace t_priv_fulldisc=1 if missing(t_fulldisclosure) & (t_slegalf=="Private limited companies" & pre_t_assets>tr_toas) // if there is no historical data in orbis, uses the recent data from Zephyr.
gen a_priv_fulldisc= (a_slegalf=="Private limited companies" & a_fulldisclosure==1)  // "fulldisclosure" uses orbis historical data to classify firms, with toas, sales and empl.
replace a_priv_fulldisc=1 if missing(a_fulldisclosure) & (a_slegalf=="Private limited companies" & pre_a_assets>tr_toas) // if there is no historical data in orbis, uses the recent data from Zephyr.
// generate dummies for legal type classification
gen t_private = (t_slegalf=="Private limited companies") 
gen t_public =(t_slegalf=="Public limited companies") 
gen t_other = (t_slegalf=="Other") 
gen t_unknown = (t_slegalf=="Unknown")
gen a_private = (a_slegalf=="Private limited companies") 
gen a_public =(a_slegalf=="Public limited companies") 
gen a_other = (a_slegalf=="Other") 
gen a_unknown = (a_slegalf=="Unknown")
// deal characteristics 
replace pre_t_assets=t_toas_orbis if missing(pre_t_assets) | pre_t_assets<0
gen pre_t_debt=pre_t_assets -pre_t_equity
gen crosscountry_priv=t_private*cross_country
gen withincountry=(cross_country==0) 
gen withincountry_priv=t_private*withincountry 
gen crossindustry_2d= (a_sic_2d!=sic_2d) 
gen crossindustry_priv_2d=t_private*crossindustry_2d
gen withinindustry_2d= (a_sic_2d==sic_2d)  
gen withinindustry_priv_2d=t_private*withinindustry_2d
gen crossindustry_3d= (a_sic_3d!=sic_3d)  
gen crossindustry_priv_3d=t_private*crossindustry_3d
gen withinindustry_3d= (a_sic_3d==sic_3d) 
gen withinindustry_priv_3d=t_private*withinindustry_3d
// merge with industry stat for classifying TARGET relative size. The code for industry stats is in another file.
joinby  country sic_2d closdate_year using "${path_project}/Data/3_financialstats-2d.dta", unm(master)
drop _m
gen t_priv_large=(pre_t_assets>=indMedian_toas) & t_private==1 & !missing(pre_t_assets) 
// Asset of  each type of target  (we will sum them in the industry level)
gen t_priv_asset=t_private*pre_t_assets  
gen t_priv_asset_fd=t_priv_fulldisc*pre_t_assets
gen t_priv_asset_large=t_priv_large*pre_t_assets
gen crosscountry_priv_asset=crosscountry_priv*pre_t_assets 
gen withincountry_priv_asset=withincountry_priv*pre_t_assets  //20201028
gen crossindustry_priv_asset_2d=crossindustry_priv_2d*pre_t_assets 
gen withinindustry_priv_asset_2d=withinindustry_priv_2d*pre_t_assets
gen crossindustry_priv_asset_3d=crossindustry_priv_3d*pre_t_assets 
gen withinindustry_priv_asset_3d=withinindustry_priv_3d*pre_t_assets  
// Asset of  each type of acquirer  (we will sum them in the industry level)
gen a_priv_asset=a_private*pre_a_assets
// RR toeholds and public targets 
gen toehold=(!missing(initial) & initial>0) if t_private==1
gen toehold_assets= toehold*pre_t_assets  
gen t_publ_asset=t_public*pre_t_assets  
save "1_zephyr_clean_extended.dta", replace

**# PART 1.1: Aggregation 2 digit
//agregating targets acquisitions for each industry (2d)
preserve   
collapse (sum) indNum_t_priv=t_private indNum_t_priv_fd=t_priv_fulldisc  ///
			indNum_t_priv_large=t_priv_large ///
			 indNum_crosscountry_priv=crosscountry_priv   ///
			 indNum_withincountry_priv=withincountry_priv  /// //2020-10-28
			indNum_crossind_priv=crossindustry_priv_2d  ///
			indNum_withinind_priv=withinindustry_priv_2d /// 
			indNum_t_priv_toe=toehold indNum_t_publ=t_public /// //2022 RR
			indSum_t_priv_asset=t_priv_asset indSum_t_priv_asset_fd=t_priv_asset_fd ///
			indSum_t_priv_asset_large=t_priv_asset_large ///
			indSum_crosscountry_priv_asset=crosscountry_priv_asset ///
			indSum_withincountry_priv_asset=withincountry_priv_asset /// //2020-10-28
			indSum_crossindustry_priv_asset=crossindustry_priv_asset_2d indSum_withinindustry_priv_asset=withinindustry_priv_asset_2d /// 
			indSum_t_priv_toe=toehold_assets indSum_t_publ=t_publ_asset /// //2022RR
			, by(country sic_2d closdate_year)
		 
gen indNum_t_priv_nonfd=indNum_t_priv-indNum_t_priv_fd
gen indSum_t_priv_asset_nonfd=indSum_t_priv_asset-indSum_t_priv_asset_fd
gen indNum_t_priv_small=indNum_t_priv-indNum_t_priv_large
gen indSum_t_priv_asset_small=indSum_t_priv_asset-indSum_t_priv_asset_large
save "1_zephyr_clean_aggregated-2d_targets.dta", replace 
restore
//agregating acquirer purchases for each industry (2d)
preserve 
collapse (sum) indNum_a_priv=a_private ///
			indSum_a_priv_asset=a_priv_asset ///
		 , by(country a_sic_2d closdate_year)
rename a_sic_2d sic_2d
//merging both agregation to have just one file
joinby country sic_2d closdate_year using "1_zephyr_clean_aggregated-2d_targets.dta", unm(both) update  //
drop _merge
replace indNum_a_priv=0 if missing(indNum_a_priv)
replace indNum_t_priv=0 if missing(indNum_t_priv)
save "1_zephyr_clean_aggregated-2d_v2.dta", replace 
erase "1_zephyr_clean_aggregated-2d_targets.dta"
restore 


**# PART 2: INDUSTRY LEVEL FINAL DATASET

cls
clear

// merging 2-digit SIC aggregation  
display("Merge of 2-digit data")
use "2_scope-2d_extended.dta", clear
rename  year closdate_year
merge 1:1 country closdate_year sic_2d using "1_zephyr_clean_aggregated-2d_v2.dta"  
drop if _merge==2                                                  // drop targeted industries  with 0 companies  in the clean universe of ORBIS 
drop _merge  
replace indNum_a_priv=0 if missing(indNum_a_priv) // for cases _merge==1
replace indNum_t_priv=0 if missing(indNum_t_priv)  // for cases _merge==1
bysort country sic_2d: egen indTSum_a= sum(indNum_a_priv)                      //total number of deals in each country-industry (later we will filter those ind with >=1 deal)
bysort country sic_2d: egen indTSum_t= sum(indNum_t_priv)                      //total number of deals in each country-industry (later we will filter those ind with >=1 deal)
gen indTSum_deals=indTSum_a+indTSum_t																				
joinby  country sic_2d closdate_year using "${path_project}/Data/3_financialstats-2d.dta", unm(master)  // the code for industry performance stats is in another file. 
drop _merge
rename closdate_year year
joinby  country sic_2d year using "${path_project}/Data/2_scope-2d_audit.dta", unm(master)       //idem for scope but now with the regulatory thresholds for the auditing requirement
drop _merge
// refilling industries with 0  deals     
replace indNum_t_priv_fd=0 if  indNum_t_priv==0
replace indNum_t_priv_nonfd=0 if indNum_t_priv==0
replace indNum_t_priv_large=0 if indNum_t_priv==0
replace indNum_t_priv_small=0 if  indNum_t_priv==0
replace indNum_crosscountry_priv=0 if  indNum_t_priv==0
replace indNum_withincountry_priv=0 if  indNum_t_priv==0
replace indNum_crossind_priv=0 if indNum_t_priv==0
replace indNum_withinind_priv=0 if indNum_t_priv==0
replace indNum_t_publ=0 if missing(indNum_t_publ)
replace indNum_t_priv_toe=0 if indNum_t_priv==0
replace indSum_t_priv_asset=0 if  indNum_t_priv==0
replace indSum_t_priv_asset_fd=0 if  indNum_t_priv==0
replace indSum_t_priv_asset_nonfd=0 if  indNum_t_priv==0
replace indSum_t_priv_asset_large=0 if  indNum_t_priv==0
replace indSum_t_priv_asset_small=0 if  indNum_t_priv==0
replace indSum_crosscountry_priv_asset=0 if  indNum_t_priv==0
replace indSum_withincountry_priv_asset=0 if  indNum_t_priv==0
replace indSum_crossindustry_priv_asset=0 if  indNum_t_priv==0
replace indSum_withinindustry_priv_asset=0 if  indNum_t_priv==0
replace indSum_a_priv_asset=0 if  indNum_a_priv==0
replace indSum_t_priv_toe=0 if indNum_t_priv==0

** creating fixed effect
egen country2= group(country )
egen sic_2d_2= group(sic_2d )
gen sic_1d=substr(sic_2d,1,1)
egen country_year= group( country year)
egen sic_2d_year= group(sic_2d year)
egen sic_1d_year= group(sic_1d year)
egen country_sic_2d=concat(country sic_2d) 
egen country_sic_2d_2= group(country_sic_2d) 
egen country_sic_1d=concat(country sic_1d) 
egen country_sic_1d_2= group(country_sic_1d)
xtset country_sic_2d_2 year, yearly
// Dependent Variables 
gen logIndNum_a_priv=log(1 + indNum_a_priv)
gen logIndNum_t_priv=log(1 + indNum_t_priv)
gen logIndNum_t_priv_fd=log(1 + indNum_t_priv_fd)
gen logIndNum_t_priv_nonfd=log(1 + indNum_t_priv_nonfd)
gen logIndNum_t_priv_large=log(1 + indNum_t_priv_large)
gen logIndNum_t_priv_small=log(1 + indNum_t_priv_small)
gen logIndNum_t_priv_toe=log(1 + indNum_t_priv_toe)
gen logIndNum_t_publ=log(1 + indNum_t_publ)
gen logIndSum_a_priv=log(1 + indSum_a_priv_asset)
gen logIndSum_t_priv=log(1 + indSum_t_priv_asset)
gen logIndSum_t_priv_fd=log(1 + indSum_t_priv_asset_fd)
gen logIndSum_t_priv_nonfd=log(1 + indSum_t_priv_asset_nonfd)
gen logIndSum_t_priv_large=log(1 + indSum_t_priv_asset_large)
gen logIndSum_t_priv_small=log(1 + indSum_t_priv_asset_small)
gen logIndNum_xcountry_priv=log(1 + indNum_crosscountry_priv)
gen logIndSum_xcountry_privasset=log( 1 +indSum_crosscountry_priv_asset)
gen logIndNum_CrossInd_priv=log(1 + indNum_crossind_priv)
gen logIndSum_CrossInd_privasset=log( 1 +indSum_crossindustry_priv_asset)
gen logIndNum_wind_priv=log(1 + indNum_withinind_priv)
gen logIndSum_wind_privasset=log( 1 +indSum_withinindustry_priv_asset)
gen privCrossCountry_ratio=indNum_crosscountry_priv/indNum_t_priv
gen privCrossCountry_assets_ratio=indSum_crosscountry_priv_asset/indSum_t_priv_asset
gen privCrossind_ratio=indNum_crossind_priv/indNum_t_priv
gen privCrossind_assets_ratio=indSum_crossindustry_priv_asset/indSum_t_priv_asset
gen privWind_ratio=indNum_withinind_priv/indNum_t_priv
gen privWind_assets_ratio=indSum_withinindustry_priv_asset/indSum_t_priv_asset
gen logIndSum_t_priv_toe=log(1 + indSum_t_priv_toe)
gen logIndSum_t_publ=log(1 + indSum_t_publ)
// Independent Variables
xtset country_sic_2d_2 year, yearly
gen scope=num_firmspriv_fulldisc/num_firmspriv
gen scope_audit=num_firmspriv_audited/num_firmspriv
gen percent_privfull= num_firmspriv_fulldisc/num_firms
gen percent_publ= num_firmspubl/num_firms    
gen percent_priv= num_firmspriv/num_firms
gen percent_privfull_toas= toas_privfull/indtoas
gen percent_publ_toas= toas_publ/indtoas
gen log_num_firms=log(1+num_firms)
gen log_indMean_toas=log(1+indMean_toas)
// redeployability
merge m:1  sic_2d year using "${path_project}/Data/Redeployability/redeployability_2d.dta"
drop if _merge==2
drop _merge
bysort year: egen redeploy_median=median(redeploy_2d)
gen redeploy_high_2000=(redeploy_2d> redeploy_median) if !missing(redeploy_2d) & year==2003
bysort country_sic_2d_2: egen redeploy_high= max(redeploy_high_2000) if !missing(redeploy_2d)
//
xtset country_sic_2d_2 year, yearly
saveold  "1_indLevel data merged-2d_extended.dta", replace 


**# PART 3:  DEAL LEVEL FINAL DATASET
cls
clear
cd "${path_RR}/Data/Zephyr_extended"
use  "1_zephyr_clean_extended.dta", clear
rename  closdate_year year
merge m:1 country year sic_2d using "2_scope-2d_extended.dta"
drop if _merge==1                                                  // drop targeted industries  with 0 companies  in the  ORBIS 
drop _merge  
rename   year closdate_year
joinby  country sic_2d closdate_year using "${path_project}/Data/3_financialstats-2d.dta", unm(master) // merging with performance and risk 
drop _merge
rename closdate_year year
// keeping just private targets
keep if t_private==1
keep if acquired==100
// creating fixed effect
egen country2= group(country )
gen sic_1d=substr(sic_2d,1,1)
egen country_year= group( country year)
egen sic_3d_year= group(sic_3d year)
egen country_sic_1d=concat(country sic_1d) 
egen country_sic_1d_2= group(country_sic_1d)
egen country_sic_2d=concat(country sic_2d) 
egen country_sic_2d_2= group(country_sic_2d) 
// Dependent variable
gen multiple=((value/acquired/100)+pre_t_debt)/pre_t_assets  
gen log_entvalue=log(1+multiple)
gen dealvalue_ebitda=value/pre_t_ebitda
replace dealvalue_ebitda=. if dealvalue_ebitda<=0
gen log_value_ebit=log(1+dealvalue_ebitda)

//  Independent Variables: SCOPE
duplicates drop deal year, force
gen scope=num_firmspriv_fulldisc/num_firmspriv
gen percent_privfull= num_firmspriv_fulldisc/num_firms
gen percent_publ= num_firmspubl/num_firms    
gen percent_priv= num_firmspriv/num_firms
gen percent_privfull_toas= toas_privfull/indtoas
gen percent_publ_toas= toas_publ/indtoas
bysort country_sic_2d (year): gen Lscope=scope[_n-1] // I need to do this because it is not a panel dataset.
bysort country_sic_2d (year): gen Lpercent_privfull_toas=percent_privfull_toas[_n-1]
bysort country_sic_2d (year): gen Lpercent_publ_toas=percent_publ_toas[_n-1]

// Independent Variables: INDUSTRY LEVEL
gen log_num_firms=log(1+num_firms)
gen log_indMean_toas=log(1+indMean_toas)
bysort country_sic_2d (year): gen Llog_num_firms=log_num_firms[_n-1]
bysort country_sic_2d (year): gen Llog_indMean_toas=log_indMean_toas[_n-1]
bysort country_sic_2d (year): gen LindMeanRw_roa=indMeanRw_roa[_n-1]
bysort country_sic_2d (year): gen LindSdRw_roa=indSdRw_roa[_n-1]
// Independent Variables: FIRM LEVEL
gen pre_t_size=ln(pre_t_assets)
gen pre_t_roa= pre_t_ebitda/ pre_t_assets
gen pre_t_lev= pre_t_debt/pre_t_assets
gen pre_a_size=ln(pre_a_assets)
gen pre_a_roa= pre_a_ebitda/ pre_a_assets
gen pre_a_lev= 1-pre_a_equity/pre_a_assets

replace pre_a_size= ln(pre_a_assets) if missing(pre_a_size)
replace pre_a_roa=a_roa if missing(pre_a_roa)
replace pre_a_lev=a_leverage if missing(pre_a_lev)
drop wpre_t_roa wpre_t_lev
winsor log_entvalue, gen(wlog_entvalue) p(0.025)
winsor log_value_ebit, gen(wlog_value_ebit) p(0.025)
winsor pre_t_roa, gen(wpre_t_roa) p(0.025)
winsor pre_t_lev, gen(wpre_t_lev) p(0.025)
winsor pre_a_roa, gen(wpre_a_roa) p(0.025)
winsor pre_a_lev, gen(wpre_a_lev) p(0.025)

xtset deal year, yearly
saveold  "2_dealLevel data merged-2d_extended.dta", replace 

**# PART 4:  PANEL 
cls
clear
cd "${path_RR}/Data/Zephyr_extended" 
use "${path_project}/Data/4_transfers_panelJuly2017.dta",clear    // panel from Francisco's Paper
// Only deals in countries and years with threshold info
keep if t_country=="AT"|t_country=="BE"|t_country=="DE"|t_country=="DK"|t_country=="ES"|t_country=="FI"|t_country=="FR"|t_country=="GB"|t_country=="IE"|t_country=="IT"|t_country=="NL"|t_country=="SE"
gen country=""
replace country="Austria"  if t_country=="AT"
replace country="Belgium"  if t_country=="BE"
replace country="Germany"  if t_country=="DE"
replace country="Denmark"  if t_country=="DK"
replace country="Spain"  if t_country=="ES"
replace country="Finland"  if t_country=="FI"
replace country="France"  if t_country=="FR"
replace country="United Kingdom"  if t_country=="GB"
replace country="Ireland"  if t_country=="IE"
replace country="Italy"  if t_country=="IT"
replace country="Netherlands"  if t_country=="NL"
replace country="Sweden"  if t_country=="SE"
// recoding industry classification
tostring(t_sic), replace
gen sic_3d=substr(t_sic,1,3)
gen sic_2d=substr(t_sic,1,2)
// Merging with scope
merge m:1 country year sic_2d using "2_scope-2d_extended.dta"   //version antigua no 
keep if _merge==3
drop _merge
// Merging with previous "Zephyr clean" file to recover targets' legal status and size classification
rename  bvid t_bvid
merge m:m t_bvid using "1_zephyr_clean_extended.dta", keepusing(t_slegalf tr* t_fulldisclosure t_priv_fulldisc t_private t_public t_other t_unknown )
drop if _merge==2
drop _merge
//keeping only private target
keep if t_private==1
duplicates drop t_bvid year, force
//merging with ind stat
rename year closdate_year
joinby  country sic_2d closdate_year using "${path_project}/Data/3_financialstats-2d.dta", unm(master) // merging with performance and risk 
drop _merge
rename closdate_year year
// creating fixed effect
egen country2= group(country )
gen sic_1d=substr(sic_2d,1,1)
egen country_year= group( country year)
egen sic_3d_year= group(sic_3d year)
egen country_sic_3d=concat(country sic_3d) 
egen country_sic_3d_2= group(country_sic_3d) 
egen country_sic_2d=concat(country sic_2d) 
egen country_sic_2d_2= group(country_sic_2d) 
egen country_sic_1d=concat(country sic_1d) 
egen country_sic_1d_2= group(country_sic_1d) 
//  Independent Variables: SCOPE
gen scope=num_firmspriv_fulldisc/num_firmspriv
gen  fulldisclosure=t_fulldisclosure==1 
gen lnturn=log(turn)   
xtset panel_id year, yearly
saveold  "5_panel data merged-2d_extended.dta", replace 

